/*
 * @author xiening
 * Running this will allow you to show white line around objects which you selected.
 */
/*
  * 需要在jsp中导入的包
  <script src="./ThreeJs/three.js"></script>
  <script src="./ThreeJs/EffectComposer.js"></script>
  <script src="./ThreeJs/RenderPass.js"></script>
  <script src="./ThreeJs/OutlinePass.js"></script>
  <script src="./ThreeJs/FXAAShader.js"></script>
  <script src="./ThreeJs/ShaderPass.js"></script>
  <script src="./ThreeJs/CopyShader.js"></script>
  */

THREE.ThreeJs_Composer = function (_renderer, _scene, _camera) {
    var raycaster = new THREE.Raycaster();
    var mouse = new THREE.Vector2();
    var composer = new THREE.EffectComposer(_renderer);
    var renderPass = new THREE.RenderPass(_scene, _camera);
    var selectedObjects = [];
    composer.addPass(renderPass);
    var outlinePass = new THREE.OutlinePass(new THREE.Vector2(window.innerWidth, window.innerHeight), _scene, _camera);
    outlinePass.edgeStrength = 5;//包围线浓度
    outlinePass.edgeGlow = 0.5;//边缘线范围
    outlinePass.edgeThickness = 2;//边缘线浓度
    outlinePass.pulsePeriod = 2;//包围线闪烁频率
    outlinePass.visibleEdgeColor.set('#ffffff');//包围线颜色
    outlinePass.hiddenEdgeColor.set('#190a05');//被遮挡的边界线颜色
    composer.addPass(outlinePass);
    var effectFXAA = new THREE.ShaderPass(THREE.FXAAShader);
    effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight);
    effectFXAA.renderToScreen = true;
    composer.addPass(effectFXAA);

    window.addEventListener('mousemove', onMouseMove);

    function onMouseMove(event) {
        var x, y;
        if (event.changedTouches) {
            x = event.changedTouches[0].pageX;
            y = event.changedTouches[0].pageY;
        } else {
            x = event.clientX;
            y = event.clientY;
        }
        mouse.x = (x / window.innerWidth) * 2 - 1;
        mouse.y = -(y / window.innerHeight) * 2 + 1;
        raycaster.setFromCamera(mouse, _camera);

        var intersects = raycaster.intersectObjects([_scene], true);
        if (intersects.length > 0 && intersects[0].object.name === "架体") {
            console.log("选择的物体："+intersects[0].object.parent.name);
            selectedObjects.pop();
            selectedObjects.push(intersects[0].object);
            outlinePass.selectedObjects = selectedObjects;//给选中的线条和物体加发光特效
        }else {
            selectedObjects.pop();
        }
    }
    return composer;
};
